<?php
/**
 * VerifycodeAction.class.php   验证码操作类
 *
 * @author                      mashanling(msl-138@163.com)
 * @date                        2012-09-27 17:25:34
 * @last modify                 2012-09-29 08:49:45 by mashanling
 */

!defined('ROOT') && exit('Access Denied');

class VerifycodeAction extends CommonAction {
    protected $auto_check_priv = false;//是否自动检测权限
    //对应模块
    private $verifycode_module = array(
    	'sys',//系统默认
    	'module_admin',//管理员模块，包括后台登陆及修改密码
    );

    /**
     * 入口
     *
     * @author       mashanling
     * @date         2012-09-27 17:26:23
     * @last modify  2012-09-29 08:49:38 by mashanling
     *
     * @return void 无返回值
     */
    function index() {
        $module = Filter::get('module', 'get');//模块
        $error  = '';

        if (!APP_DEBUG) {//非调试模式
            if (!isset($_SERVER['HTTP_REFERER'])) {
                $error = 'HTTP_REFERER' . L('IS_EMPTY');
            }

            elseif (strpos($_SERVER['HTTP_REFERER'], WEB_SITE_URL) === false) {
                $error = 'HTTP_REFERER(' . $_SERVER['HTTP_REFERER'] . ')' . L('IS_EMPTY');
            }
        }

        if (!$error) {
            if (!$module) {
                $error = 'module' . L('IS_EMPTY');
            }
            elseif (!in_array($module, $this->verifycode_module)) {
                $error = 'module not in (' . join(',', $this->verifycode_module) . ')';
            }
            else {
                $verifycode_setting  = get_verifycode_setting($module);//验证码设置

                //未开启验证码
                if (!$verifycode_setting['enable']) {
                    $default_setting     = get_verifycode_setting('sys', 'enable');//默认设置
                    $error = L('NOT_HAS,TURN_ON') . ("(module:{$verifycode_setting['enable']}|sys:{$default_setting})");
                }
            }
        }

        if ($error) {//有错误
            $this->model->addLog(L('VERIFY_CODE') . "({$module})" . $error, LOG_TYPE_VERIFYCODE_ERROR);
            exit();
        }
        elseif (!check_verifycode_limit($module, 'refresh')) {//刷新次数限制
            return false;
        }

        $width  = $verifycode_setting['width'];//宽
        $height = $verifycode_setting['height'];//高
        $length = $verifycode_setting['length'];//字母长
        $type   = $verifycode_setting['type'];//类型

        Image::buildVerifyImage($verifycode_setting['length'], $verifycode_setting['type'], $verifycode_setting['width'], $verifycode_setting['height']);
    }
}